package org.idea.spring.bean.listener;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.stereotype.Component;

/**
 * 在容器初始化之前，往容器里面注入相应的bean
 * @author linhao
 * @date created in 7:40 下午 2020/10/11
 */
@Component
public class MyBeanDefinitionRegistryPostProcessor implements BeanDefinitionRegistryPostProcessor {

    @Override
    public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
        System.out.println("容器启动之前，进行相关的注册");
        System.out.println("启动容器之前，已有bean的数目为："+registry.getBeanDefinitionCount());
        for (String beanDefinitionName : registry.getBeanDefinitionNames()) {
            //这里面正好展示的是启动spring容器的register函数中初始化注册的几个beandefinition
            System.out.println(beanDefinitionName);
        }
        RootBeanDefinition rootBeanDefinition = new RootBeanDefinition();
//        registry.registerBeanDefinition(rootBeanDefinition);
    }

    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {

    }
}
